package com.yingwei.mining.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yingwei.mining.control.Constant;
import com.yingwei.mining.model.businessobject.Edge;
import com.yingwei.mining.model.businessobject.Excavator;
import com.yingwei.mining.model.businessobject.Node;
import com.yingwei.mining.model.businessobject.Truck;
import com.yingwei.mining.model.businessobject.UnloadArea;
import com.yingwei.mining.model.businessobject.WorkOrder;
import com.yingwei.mining.util.LocationUtil;

public class ExcavatorStatusPanel extends MapDeskTopPane
{
    private static final long serialVersionUID = -6906854448381341709L;
    private static Logger logger = LoggerFactory
	    .getLogger(ExcavatorStatusPanel.class);
    private static boolean showId = false;

    public ExcavatorStatusPanel()
    {
    }

    protected void paintComponent(Graphics g)
    {
	paintWorkOrders(g);
    }

    public void paintTruck(Graphics g, Truck tr)
    {
	int x = LocationUtil.getScreenLocationX(tr.getX());
	int y = LocationUtil.getScreenLocationY(tr.getY());

	Image img = truckImage;
	FontMetrics fm = g.getFontMetrics();
	int w = fm.stringWidth("" + tr.getId()) + 10;
	int h = fm.getHeight() + 4;
	if (showIcon)
	{
	    if (img != null)
	    {
		g.drawImage(img, x, y, this);
	    }
	}
	else
	{
	    g.setColor(Constant.TEXT_COLOR);
	    g.fillRect(x - w / 2, y - h / 2, w, h);
	    g.setColor(Constant.truckColor);
	    g.drawRect(x - w / 2, y - h / 2, w - 1, h - 1);
	}
	g.setColor(Color.white);
	g.drawString("" + tr.getId(), x - (w - 10) / 2, (y - (h - 4) / 2)
		+ fm.getAscent());
	if (img != null)
	{
	    g.drawImage(img, x - w / 2, y + h / 2, this);
	    g.drawString(tr.getName(), x - w / 2, y + 2 * h);
	}
    }

    public void paintWorkOrders(Graphics g)
    {
	int topLeftX = 10;
	int topLeftY = 60;
	int rowCnt = 0;
	int colCnt = 0;
	int rowHeight = 50;
	int colWidth = 65;
	int trucksPerLine = 15;

	drawLegend(g);
	g.setColor(Constant.BACKGROUND_COLOR);
	for (Excavator ex : data.getExcavators())
	{
	    if (!ex.getStatus().getStatusId().equalsIgnoreCase(
		    Constant.EXCAVATOR_STATUS_INACTIVE))
	    {
		if (ex.getStatus().getStatusId().equalsIgnoreCase(
			Constant.EXCAVATOR_STATUS_BUSY))
		{
		    g.setColor(Constant.COLOR_LOAD);
		}
		else
		{
		    g.setColor(Constant.COLOR_WAIT_LOAD);
		}
		g.drawImage(excavatorImage, topLeftX, topLeftY + rowCnt
			* rowHeight, this);
		g.drawString((showId ? (ex.getId() + ":") : "") + ex.getName(),
			topLeftX, topLeftY + rowCnt * rowHeight + 35);
		colCnt = 0;
		for (WorkOrder wo : data.getWorkOrders())
		{
		    if (wo.getStatus().equalsIgnoreCase(
			    Constant.WORK_ORDER_STATUS_SCHEDULED)
			    && ex.getId() == wo.getExcavator().getId()
			    && (wo.getTruck().getStatus().getStatusId()
				    .equalsIgnoreCase(
					    Constant.TRUCK_STATUS_WAIT_LOAD)
				    || wo.getTruck().getStatus().getStatusId()
					    .equalsIgnoreCase(
						    Constant.TRUCK_STATUS_LOAD) || wo
				    .getTruck().getStatus().getStatusId()
				    .equalsIgnoreCase(
					    Constant.TRUCK_STATUS_MOVE_TO_LOAD)))
		    {
			if (wo.getTruck().getStatus().getStatusId()
				.equalsIgnoreCase(Constant.TRUCK_STATUS_LOAD))
			{
			    g.setColor(Constant.COLOR_LOAD);
			}
			else if (wo.getTruck().getStatus().getStatusId()
				.equalsIgnoreCase(
					Constant.TRUCK_STATUS_WAIT_LOAD))
			{
			    g.setColor(Constant.COLOR_WAIT_LOAD);
			}
			else if (wo.getTruck().getStatus().getStatusId()
				.equalsIgnoreCase(
					Constant.TRUCK_STATUS_MOVE_TO_LOAD))
			{
			    g.setColor(Constant.COLOR_MOVE_TO_LOAD);
			}
			else
			{
			    g.setColor(Constant.COLOR_INACTIVE);
			}
			if (colCnt > trucksPerLine)
			{
			    colCnt = 0;
			    rowCnt++;
			}
			g
				.drawImage(truckImage, topLeftX + 60 + colCnt
					* colWidth, topLeftY + rowCnt
					* rowHeight, this);
			g.drawString((showId ? (wo.getTruck().getId() + ":")
				: "")
				+ wo.getTruck().getName(), topLeftX + 60
				+ colCnt * colWidth, topLeftY + rowCnt
				* rowHeight + 35);
			colCnt++;
		    }
		}
		rowCnt++;
	    }
	}
	for (Excavator ex : data.getExcavators())
	{
	    if (ex.getStatus().getStatusId().equalsIgnoreCase(
		    Constant.EXCAVATOR_STATUS_INACTIVE))
	    {
		g.drawImage(excavatorImage, topLeftX, topLeftY + rowCnt
			* rowHeight, this);
		g.setColor(Constant.COLOR_INACTIVE);
		g.drawString((showId ? (ex.getId() + ":") : "") + ex.getName(),
			topLeftX, topLeftY + rowCnt * rowHeight + 35);
		rowCnt++;
	    }
	}
    }
}
